unit movDeposito;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, DBGrids, RXDBCtrl, StdCtrls, Mask, ToolEdit, Provider,
  SqlExpr, DB, DBClient, DBLocal, DBLocalS, DateUtils, Buttons, Menus;

type
  TfrmMovDeposito = class(TForm)
    lblFechaIni: TLabel;
    edtFechaIni: TDateEdit;
    lblFechaFin: TLabel;
    edtFechaFin: TDateEdit;
    grd: TRxDBGrid;
    cds: TSQLClientDataSet;
    s: TDataSource;
    cdsAPENOM: TStringField;
    cdsFECHA: TSQLTimeStampField;
    cdsMATERIAL: TStringField;
    cdsDENOMINACION: TStringField;
    cdsCANTIDAD: TFloatField;
    cdsORIGEN: TStringField;
    cdsDIRECCION: TStringField;
    btnExcel: TSpeedButton;
    btnCargar: TBitBtn;
    Label3: TLabel;
    filtroExtra: TEdit;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    cdsSENTIDO: TIntegerField;
    cdsID_MOVDEPOSITO: TIntegerField;
    PopupMenu1: TPopupMenu;
    Editar1: TMenuItem;
    cdsID_MATERIAL: TIntegerField;
    cdsOBSERVACIONES: TMemoField;
    cdsobs: TStringField;
    Borrarregistro1: TMenuItem;
    procedure btnCargarClick(Sender: TObject);
    procedure grdCellClick(Column: TColumn);
    procedure btnExcelClick(Sender: TObject);
    procedure filtroExtraKeyPress(Sender: TObject; var Key: Char);
    procedure SpeedButton2Click(Sender: TObject);
    procedure grdDrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure grdTitleClick(Column: TColumn);
    procedure Editar1Click(Sender: TObject);
    procedure cdsCalcFields(DataSet: TDataSet);
    procedure Borrarregistro1Click(Sender: TObject);
  private
    FcolumnaSeleccionada: word;
    procedure SetcolumnaSeleccionada(const Value: word);
    { Private declarations }
  published
    property columnaSeleccionada: word read FcolumnaSeleccionada write SetcolumnaSeleccionada;
  public
    constructor CreateNuevo(AOwner: TComponent);
    procedure refrescar;

  end;

var
  frmMovDeposito: TfrmMovDeposito;

implementation

uses Recursos, deposito, retiroMaterial, MDatos;

{$R *.dfm}

procedure TfrmMovDeposito.btnCargarClick(Sender: TObject);
begin
  self.cds.Close;
  self.cds.Params[0].AsString := FormatDateTime('mm/dd/yy', StrToDate(self.edtFechaIni.EditText));
  self.cds.Params[1].AsString := FormatDateTime('mm/dd/yy', incDay(StrToDate(self.edtFechaFin.EditText)));
  self.cds.Open;
end;

constructor TfrmMovDeposito.CreateNuevo(AOwner: TComponent);
begin
  inherited Create(Aowner);
  self.edtFechaIni.Date := IncYear(now, -1);
  self.edtFechaFin.Date := now;
  self.ShowModal;
end;

procedure TfrmMovDeposito.SetcolumnaSeleccionada(const Value: word);
begin
  FcolumnaSeleccionada := Value;
end;

procedure TfrmMovDeposito.grdCellClick(Column: TColumn);
var
  i: byte;
begin
  //if (Column.Title.Font.Color <> clRed) and (Column.Field.DataType = ftString)then
  if (Column.Title.Font.Color <> clRed) and (Column.Field.DataType <> ftTimeStamp) then
  begin
    for i := 0 to self.grd.Columns.Count-1 do
      self.grd.Columns [i].Title.Font.Color := Column.Title.Font.Color;
    Column.Title.Font.Color := clRed;
    self.columnaSeleccionada := self.grd.Col-1;
  end;

end;

procedure TfrmMovDeposito.btnExcelClick(Sender: TObject);
begin
  abrirEnExcel(self, self.cds, nil, nil, 'LISTADO DE MATERIALES', 'INTERVALO DEFINIDO: del '
  +DateTimeToStr(self.edtFechaIni.Date)+' al '+DateTimeToStr(self.edtFechaFin.Date));
end;

procedure TfrmMovDeposito.filtroExtraKeyPress(Sender: TObject;
  var Key: Char);
begin
  if (Key = chr(VK_RETURN)) then
  begin
    if (self.filtroExtra.Text <> '') then
    begin
      if (self.grd.Columns[self.columnaSeleccionada].Field.DataType = ftString) then
        self.cds.Filter := self.grd.Columns [self.columnaSeleccionada].FieldName+' LIKE ''%'+self.filtroExtra.Text+'%'''
      else
        self.cds.Filter := self.grd.Columns [self.columnaSeleccionada].FieldName+' = '+self.filtroExtra.Text;
      self.cds.Filtered := True;
    end
    else self.cds.Filtered := False;
  end;
end;

procedure TfrmMovDeposito.SpeedButton2Click(Sender: TObject);
begin
  self.grd.Font.Size := self.grd.Font.Size + TSpeedButton(Sender).tag;
end;

procedure TfrmMovDeposito.grdDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
const
  clPaleGreen = TColor($00DAEED7);
  clPaleRed =   TColor($00D3DAF1);
begin
    if (Column.Field.Dataset.FieldbyName('SENTIDO').AsInteger = 1) then
      grd.canvas.brush.color := clPaleGreen
    else  grd.canvas.brush.color := clPaleRed;
      //if (gdFocused in State) then grd.canvas.brush.color := clPaleGreen
      //else grd.canvas.brush.color := clPaleRed;
    grd.DefaultDrawColumnCell(rect,DataCol,Column,State);
end;

procedure TfrmMovDeposito.grdTitleClick(Column: TColumn);
begin
  self.cds.IndexFieldNames := Column.FieldName+'; FECHA';
end;

procedure TfrmMovDeposito.Editar1Click(Sender: TObject);
begin
  if self.cdsSENTIDO.AsInteger = 1 then
    TfrmDeposito.CreateNuevo(self, 0, 0, self.cdsMATERIAL.AsString, self.cdsID_MOVDEPOSITO.AsInteger)
  else
    TfrmRetiroMaterial.CreateNuevo(self, 0, self.cdsID_MATERIAL.AsInteger, self.cdsID_MOVDEPOSITO.AsInteger);
end;

procedure TfrmMovDeposito.refrescar;
begin
  self.cds.Refresh;
end;

procedure TfrmMovDeposito.cdsCalcFields(DataSet: TDataSet);
begin
  self.cdsobs.AsString := self.cdsOBSERVACIONES.AsString;
end;

procedure TfrmMovDeposito.Borrarregistro1Click(Sender: TObject);
var
  ds: TSQLDataSet;
begin
  ds := TSQLDataSet.Create(self);
  try
    ds.SQLConnection := Datos.Con;
    ds.CommandText := 'DELETE from MOV_DEPOSITO where ID_MOVDEPOSITO = '+self.cdsID_MOVDEPOSITO.AsString;
    ds.ExecSQL();
  finally
    ds.Free;
  end;
end;

end.
